from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
eduOff = Api()

class EduOff1(Resource):
    def get(self):
        task = EduOff.query.all()
        return jsonify({"eduoff1": list(map(con8, task))})
    def post(self, uid):
        eduoff_add_one = EduOff(
            request.json.get('time'),
            request.json.get('usewhere'),
            request.json.get('offobject'),
            request.json.get('people_make'),
            request.json.get('people_check'),
            request.json.get('money'),
            request.json.get('off_style'),
            request.json.get('get_object'),
            request.json.get('number'),
            request.json.get('beizhu')
        )
        db.session.add(eduoff_add_one)
        db.session.commit()
        return jsonify({'eduoff_add_one': con8(eduoff_add_one)})
eduOff.add_resource(EduOff1, "/EduOff1/")


class EduOff2(Resource):
    def get(self, uid):
        task = EduOff.query.filter_by(id=uid).first()
        if task is None:
            abort(404)
        return jsonify({'eduoff2': con8(task)})

    def put(self, uid):
        eduoff_update = EduOff.query.get(uid)
        eduoff_update.time = request.json.get('time')
        eduoff_update.usewhere = request.json.get('usewhere')
        eduoff_update.offobject = request.json.get('offobject')
        eduoff_update.people_make = request.json.get('people_make')
        eduoff_update.people_check = request.json.get('people_check')
        eduoff_update.money = request.json.get('money')
        eduoff_update.off_style = request.json.get('off_style')
        eduoff_update.getobject = request.json.get('getobject')
        eduoff_update.number = request.json.get('number')
        eduoff_update.beizhu = request.json.get('beizhu')

        db.session.add(eduoff_update)
        db.session.commit()
        return jsonify({'eduoff_update': con8(eduoff_update)})


eduOff.add_resource(EduOff2, "/EduOff2/<int:uid>/")